#include "widget.h"
#include "ui_widget.h"
#include <vector>
#include "randomthread.h"
#include <QDebug>
#include <QThreadPool>

struct WidgetPrivate
{
    std::vector<int> nums;
};

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),p(new WidgetPrivate)
{
    ui->setupUi(this);
#if 0
    //创建线程对象
    RandomThread *r = new RandomThread;
    SortThread *s = new SortThread;

    connect(ui->pushButton_Random, &QPushButton::clicked,[this,r]()
    {
        r->start();//线程启动
    });
    //跨线程通信:信号和槽不在同一个线程里,绑定方式为队列连接方式
    //connect第五个参数代表由QT自行判断采取什么连接方式
    connect(r, &RandomThread::Generate,[this](const std::vector<int>& nums)
    {
        for(auto & num : p->nums)
        {
            ui->listWidget_Random->addItem(QString::number(num));
        }
        p->nums = nums;
    });

    //线程结束信号
    connect(r, &RandomThread::finished,r,&RandomThread::deleteLater);


    connect(ui->pushButton_Sort,&QPushButton::clicked,[this,s]()
    {
        s->SetNums(p->nums);
        s->start();
    });

    connect(s,&SortThread::SortFinished,[this](const std::vector<int>& nums)
    {
        for(auto & num : p->nums)
        {
            ui->listWidget_Sort->addItem(QString::number(num));
        }
    });

    connect(s,&SortThread::finished,r,&SortThread::deleteLater);
#endif
#if 0
    qDebug() <<"ui"<<QThread::currentThreadId() <<endl;
    QThread * t1 = new QThread;
    QThread * t2 = new QThread;

    ThreadObj *obj1 = new ThreadObj;
    obj1->moveToThread(t1);     // 把对象放入线程中
    connect(t1, &QThread::started, [this, obj1]()
    {
        obj1->Generate();       // 线程启动， 执行生成函数
        // 由于对象在线程内，所以执行的函数在线程内
    });
    connect(obj1, &ThreadObj::GenerateFinished, [this](const std::vector<int>& nums)
    {
        for(auto & num : nums)
        {
         ui->listWidget_Random->addItem(QString::number(num));
        }
        p->nums = nums;
    });

    connect(ui->pushButton_Random,&QPushButton::clicked,[this, t1]()
    {
        t1->start();    // 线程启动
    });
    connect(t1, &QThread::finished,t1,&QThread::deleteLater);
    connect(t1, &QThread::finished,obj1,&QThread::deleteLater);



    ThreadObj *obj2 = new ThreadObj;
    obj2->moveToThread(t2);
    connect(t2, &QThread::started, [this, obj2]()
    {
        obj2->SetNums(p->nums);
        obj2->Sort();       // 线程启动， 执行生成函数
        // 由于对象在线程内，所以执行的函数在线程内
    });
    connect(obj2, &ThreadObj::SortFinished, [this](const std::vector<int>& nums)
    {
        for(auto & num : nums)
        {
         ui->listWidget_Sort->addItem(QString::number(num));
        }
        p->nums = nums;
    });
    connect(ui->pushButton_Sort,&QPushButton::clicked,[this, t2]()
    {
        t2->start();    // 线程启动
    });
    connect(t2, &QThread::finished,t2,&QThread::deleteLater);
    connect(t2, &QThread::finished,obj2,&QThread::deleteLater);
#endif
    /* 用QT线程池 栈 */
    RandomRunnable * r = new RandomRunnable;
    SortRunnable * s = new SortRunnable;
    /* 发送按键信号 */
    connect(ui->pushButton_Random,&QPushButton::clicked,[this, r]()
    {
        QThreadPool::globalInstance()->start(r);    // 线程启动
    });
    connect(r, &RandomRunnable::GenerateFinished, [this](const std::vector<int>& nums)
    {
        for(auto & num :nums)
        {
         ui->listWidget_Random->addItem(QString::number(num));
        }
        p->nums = nums;
    });

    connect(ui->pushButton_Sort,&QPushButton::clicked,[this, s]()
    {
        s->SetNums(p->nums);
        QThreadPool::globalInstance()->start(s);    // 线程启动
    });
    connect(s, &SortRunnable::SortFinished, [this](const std::vector<int>& nums)
    {
        for(auto & num :nums)
        {
         ui->listWidget_Sort->addItem(QString::number(num));
        }

    });

}

Widget::~Widget()
{
    delete ui;
    delete p;
}


